package com.maiji.cloud;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.maiji.cloud.entities.community.*;
import com.maiji.cloud.mapper.ComMidActMapper;
import com.maiji.cloud.mapper.ComMidLabelMapper;
import com.maiji.cloud.mapper.ComUserRelationMapper;
import com.maiji.cloud.service.ComCommentService;
import com.maiji.cloud.service.CommunityService;
import com.maiji.cloud.utils.ComLabelEnum;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UpdateUserRelationType {

    @Autowired
    private ComUserRelationMapper userRelationMapper;
    @Autowired
    private ComCommentService comCommentService;
    @Autowired
    private CommunityService communityService;
    @Autowired
    private ComMidActMapper comMidActMapper;
    @Autowired
    private ComMidLabelMapper comMidLabelMapper;


    @Test
    public void test4(){
        ComMsgInfo querymsginfo = new ComMsgInfo();
        EntityWrapper entityWrapper = new EntityWrapper(querymsginfo);
        List<ComMsgInfo> msgInfos = communityService.selectList(entityWrapper);
        ArrayList<ComMiddleLabel> middleLabels = new ArrayList<>();
        for (ComMsgInfo msgInfo:msgInfos
             ) {
            if(msgInfo.getPriority() == 2){
                ComMiddleLabel middleLabel = new ComMiddleLabel();
                middleLabel.setLabelId(ComLabelEnum.ComLabelEnum_Cs.getValue());
                middleLabel.setIsShow(ComLabelEnum.ComLabelEnum_HP.getIsShow());
                middleLabel.setPublishTime(msgInfo.getPublishTime());
                middleLabel.setMsgId(msgInfo.getMsgId());
                comMidLabelMapper.insert(middleLabel);
            }else if(msgInfo.getPriority() == 3){
                ComMiddleLabel middleLabel = new ComMiddleLabel();
                middleLabel.setLabelId(ComLabelEnum.ComLabelEnum_HP.getValue());
                middleLabel.setIsShow(ComLabelEnum.ComLabelEnum_HP.getIsShow());
                middleLabel.setPublishTime(msgInfo.getPublishTime());
                middleLabel.setMsgId(msgInfo.getMsgId());
                comMidLabelMapper.insert(middleLabel);

                ComMiddleLabel middleLabel2 = new ComMiddleLabel();
                middleLabel2.setLabelId(ComLabelEnum.ComLabelEnum_Ce.getValue());
                middleLabel2.setPublishTime(msgInfo.getPublishTime());
                middleLabel2.setMsgId(msgInfo.getMsgId());
                comMidLabelMapper.insert(middleLabel2);

            }
        }



    }

    @Test
    public void test3(){
        EntityWrapper entityWrapper = new EntityWrapper(new ComMiddleActivity());
        List<ComMiddleActivity> comMiddleActivityList =  comMidActMapper.selectList(entityWrapper);
        for (ComMiddleActivity comMiddleActivity:comMiddleActivityList
             ) {
            ComMsgInfo msgInfo = communityService.selectById(comMiddleActivity.getMsgId());
            EntityWrapper entityWrapper2 = new EntityWrapper(comMiddleActivity);
            ComMiddleActivity middleActivity = new ComMiddleActivity();
            BeanUtils.copyProperties(comMiddleActivity,middleActivity);
            middleActivity.setPublishTime(msgInfo.getPublishTime());
            comMidActMapper.update(middleActivity,entityWrapper2);

        }
    }

    @Test
    public void test2 (){
        EntityWrapper<ComMsgInfo> entityWrapper = new EntityWrapper<>();
        entityWrapper.setSqlSelect("msg_id AS msgId,user_id AS userId,title,content,publish_time AS publishTime,`type`,show_location AS showLocation," +
                "only_mine AS onlyMine,commented_count,comment_count,priority,page_view AS pu,praise_count,transmit_count,collect_count,location_id AS locationId,`delete`,is_top AS isTop,top_sort AS topSort,is_integral AS isIntegral,is_report AS isReport,report_count, if(priority=1, 3, 0) as abc");
        entityWrapper.orderBy("abc", false);
        List<ComMsgInfo> comMsgInfos = communityService.selectList(entityWrapper);
        System.out.println(comMsgInfos);
    }

    @Test
    public void test1 (){
        EntityWrapper<ComComment> entityWrapper = new EntityWrapper<>();
        entityWrapper.setSqlSelect("belong_id, count(belong_id) count_not_read");
        entityWrapper.in("status", Arrays.asList(0, 1));
        entityWrapper.groupBy("belong_id");
        entityWrapper.orderBy("count_not_read", false);
        Page<Map<String, Object>> mapPage = comCommentService.selectMapsPage(new Page(1, 5), entityWrapper);
        System.out.println(mapPage);
    }

    @Test
    public void test(){
        List<ComUserRelation> userRelations = userRelationMapper.findAll();
        for (ComUserRelation user1:userRelations
             ) {
            for (ComUserRelation user2:userRelations
                 ) {
                if(user1.getFollowerId().equals(user2.getFocuserId()) && user1.getFocuserId().equals(user2.getFollowerId())){
                    System.out.println("user1" + "follower:" + user1.getFollowerId() + "focuser:" + user1.getFocuserId());
                    System.out.println("user2" + "follower:" + user2.getFollowerId() + "focuser:" + user2.getFocuserId());

                    EntityWrapper<ComUserRelation> entityWrapper = new EntityWrapper<>();
                    entityWrapper.setEntity(user1);
                    userRelationMapper.update(new ComUserRelation().setType(1),entityWrapper);

                }else{
//                    EntityWrapper<ComUserRelation> entityWrapper = new EntityWrapper<>();
//                    entityWrapper.setEntity(user1);
//                    userRelationMapper.update(new ComUserRelation().setType(0),entityWrapper);
                }
            }
        }
    }

}
